//
//  PhotoViewerVC.m
//  ygzj
//
//  Created by guoshiqing on 16/4/6.
//  Copyright © 2016年 Shenzhen Dongbao Infomation Technology Co.,Ltd. All rights reserved.
//

#import "PhotoViewerVC.h"
#import "UIImageView+WebCache.h"
#import "HYYPhotoScrollerV.h"
@interface PhotoViewerVC () <UIScrollViewDelegate>
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) UILabel *pageNumLabel;

@property (nonatomic, weak) UIScrollView *scrollView;

@end

#ifndef RESOURCE_LOCAL
#define RESOURCE_LOCAL 0
#define RESOURCE_NET 1
#endif //RESOURCE_LOCAL

@implementation PhotoViewerVC


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 滚动到指定的位置
    [self.scrollView setContentOffset:CGPointMake(self.index*UI_SCREEN_WIDTH, 0) animated:YES];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = NSLocalizedString(@"CloudDisk_PhotoInfo", @"图片详情");
    self.view.backgroundColor = [UIColor blackColor];
    
    // 创建一个scrollView
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT)];
    
    if (@available(iOS 11.0, *))
    scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    // 添加图片到scrollView中
    CGFloat scrollW = UI_SCREEN_WIDTH;
    CGFloat scrollH = UI_SCREEN_HEIGHT - NavBar_Height;
    for (int i = 0; i < self.dataList.count; i++) {
        
        if (RESOURCE_NET == self.type) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * scrollW, 0, scrollW, scrollH)];

            NSString *url = [self.dataList objectAtIndex:i];
            [imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil options:(SDWebImageRetryFailed | SDWebImageAllowInvalidSSLCertificates)];
            // 设置图片的填充模式, 网络图片也可以用
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            imageView.clipsToBounds = YES;
            [self.scrollView addSubview:imageView];
        }
        else if (RESOURCE_LOCAL == self.type)
        {
            UIImage *image = (UIImage *)(self.dataList[i]);
            HYYPhotoScrollerV *scrollerImageV = [[HYYPhotoScrollerV alloc] initWithFrame:CGRectMake(i * scrollW, 0, scrollW, scrollH) image:image];
            [self.scrollView addSubview:scrollerImageV];
        }
    }
    
    // 如果想要某个方向上不能滚动，那么这个方向对应的尺寸数值传0即可
    self.scrollView.contentSize = CGSizeMake(self.dataList.count * scrollW, 0);
    self.scrollView.bounces = NO; // 去除弹簧效果
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    
    ///////////////////////////////////////
    // 分页控制器，查看当前是第几页
    self.pageControl = [[UIPageControl alloc] init];
    self.pageControl.numberOfPages = self.dataList.count;
    self.pageControl.backgroundColor = [UIColor redColor];
//    self.pageControl.currentPageIndicatorTintColor = RGBA(253, 98, 42, 1);
//    self.pageControl.pageIndicatorTintColor = RGBA(189, 189, 189, 1);
    
    // 设置中心为位置
    CGPoint center = self.pageControl.center;
    center.x = scrollW * 0.5;
    center.y = scrollH - 50;
    self.pageControl.center = center;
    
    [self.view addSubview:self.pageControl];
    
    // 页码标签1/9
    if (self.isShowPageNumLabel) {
        self.pageNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, UI_CONTENT_HEIGHT-49.0f, UI_SCREEN_WIDTH, 49.0f)];
        self.pageNumLabel.backgroundColor = [UIColor clearColor];
        self.pageNumLabel.textAlignment = NSTextAlignmentCenter;
        self.pageNumLabel.textColor = [UIColor whiteColor];
        self.pageNumLabel.text = [NSString stringWithFormat:@"%d/%ld",(int)(self.index + 1),(long)self.dataList.count];
        
        [self.view addSubview:self.pageNumLabel];
    }
    
    // 右上角的删除按钮
    if (self.isShowDeleBtn) {
        UIBarButtonItem *deleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(onTrashClick:)];
        [deleItem setImage:[UIImage imageNamed:@"icon_delete"]];
        self.navigationItem.rightBarButtonItem = deleItem;
        [self createNavBarRightButtonWithImageName:@"icon_delete"];
        
        WEAKSELF
        [self setRightNavButtonBlock:^{
            [weakSelf onTrashClick:nil];
        }];
    }
    
    if (self.isTapDismissVC) {
        // 添加手势，点击退出
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissVC:)];
        [self.view addGestureRecognizer:tapRecognizer];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    double page = scrollView.contentOffset.x / scrollView.frame.size.width;
    // 四舍五入计算出页码
    self.pageControl.currentPage = (int)(page + 0.5);
    
    if (self.pageNumLabel) {
        self.pageNumLabel.text = [NSString stringWithFormat:@"%d/%ld",(int)(page + 0.5)+1,(long)self.dataList.count];
    }
}

#pragma mark - 手势回调，退出
- (void)dismissVC:(UITapGestureRecognizer *)tap
{
    [self dismissViewControllerAnimated:NO completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onTrashClick:(id)sender {
    //TODO: 删除
    if (RESOURCE_LOCAL == self.type) {
        NSInteger currentPage = self.pageControl.currentPage;
        
        NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:self.dataList];
        [arr removeObjectAtIndex:currentPage];
        
        NSMutableArray *indexs = [[NSMutableArray alloc] initWithArray:self.ablumIndexs];
        NSInteger index = [[indexs objectAtIndex:currentPage] integerValue];
        if (self.delegate && [self.delegate respondsToSelector:@selector(unSelectAblumObject:)]) {
            [self.delegate unSelectAblumObject:index];
        }
        if (self.delegate && [self.delegate respondsToSelector:@selector(deletePhotoIndex:)]) {
            [self.delegate deletePhotoIndex:currentPage];
        }
        [indexs removeObjectAtIndex:currentPage];
        
        self.ablumIndexs = [indexs copy];
        self.dataList = [arr copy];
        
        [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        for (int i = 0; i < self.dataList.count; i++) {
            UIImage *image = [self.dataList objectAtIndex:i];
            HYYPhotoScrollerV *scrollerImageV = [[HYYPhotoScrollerV alloc] initWithFrame:CGRectMake(i * UI_SCREEN_WIDTH, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT - 64) image:image];
            
            [self.scrollView addSubview:scrollerImageV];
        }
        
        self.scrollView.contentSize = CGSizeMake(self.dataList.count * UI_SCREEN_WIDTH, 0);
        self.pageControl.numberOfPages = self.dataList.count;
        if (self.dataList.count < 1){
            //如果只有一张图片则返回选择页面
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            double page = self.scrollView.contentOffset.x / self.scrollView.frame.size.width;
            
            self.pageControl.currentPage = (int)(page + 0.5);
            
            if (self.pageNumLabel) {
                self.pageNumLabel.text = [NSString stringWithFormat:@"%d/%ld",(int)(page + 0.5)+1,(long)self.dataList.count];
            }
        }
    }
}

@end
